import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
// 定义统一常量
const TITLE = '交投康养人脸识别系统';
const routes = [
  { path: '/', component: () => import('@/views/login/index.vue'), meta: { title: TITLE } },
  { path: '/face-login', component: () => import('@/views/login2/index.vue'), meta: { title: TITLE } },
  {
    path: '/home',
    component: () => import('@/views/home/index.vue'),
    meta: {
      title: TITLE
    },
    redirect: '/desktop',
    children: [
      { path: '/desktop', component: () => import('@/components/desktop/index') , meta: { title: TITLE }},
      { path: '/faceList', component: () => import('@/components/face/index') , meta: { title: TITLE }},
      { path: '/faceLog', component: () => import('@/components/faceLog/index') , meta: { title: TITLE }},
    ]
  }
];
const router = new VueRouter({ mode: 'history',
  base: process.env.BASE_URL,routes });
// router.beforeEach((to, from, next) => (to.path === '/' || localStorage.getItem('face_token')) ? next() : next('/'));
// 路由切换时更新标题，并进行路由权限判断
router.beforeEach((to, from, next) => {
  // 先判断是否需要更新标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  // 再进行路由权限判断
  if (to.path === '/' || localStorage.getItem('face_token')) {
    next()
  } else {
    next('/')
  }
})
export default router;
